#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{
    leveldb::DB* db;
    leveldb::Options options;
    options.create_if_missing = true;
    leveldb::Status status = leveldb::DB::Open(options,"/tmp/testdb", &db);
    assert(status.ok());


        
    ifstream list_file("./test.txt");
    string temp_file;
    char file_name[100];
    int index = 0;

    while(getline(list_file,temp_file))
    {

        temp_file = "/home/lanve/test_data/" + temp_file; 
//        cout << temp_file << endl;
//        continue;

        ifstream infile(temp_file.c_str());
        string temp;
    
        while(getline(infile,temp))
        {
            std::string host=temp.substr(2 , temp.length() - 2 );
            status = db->Put(leveldb::WriteOptions(), host,"test");
            assert(status.ok());
            index++;

            //cout<< index++ << " " <<host<< endl;
        }
        //infile.close();
        cout << "Success write a file ,index = " << index << endl;
    }
    //list_file.close();
    return 0;
}
    //write key1,value1
//    std::string key="lanve";
//    std::string value = "PowerAll";
//    
//
//    std::string key2 = "lanve2";
//    std::string value2 = "PowerAll2";
//    status = db->Put(leveldb::WriteOptions(), key,value);
//    assert(status.ok());
//    status = db->Put(leveldb::WriteOptions(), key2,value2);
//    assert(status.ok());
//    status = db->Get(leveldb::ReadOptions(), key2, &value2);
//    assert(status.ok());
//    std::cout<<key<<":"<<value2<<std::endl;

    //std::string key2 = "key2";
    //move the value under key to key2
    //status = db->Put(leveldb::WriteOptions(),key2,value);
    //assert(status.ok());
    //status = db->Delete(leveldb::WriteOptions(), key);
    //assert(status.ok());
    //status = db->Get(leveldb::ReadOptions(),key2, &value);
    //assert(status.ok());
    //std::cout<<key2<<"==="<<value<<std::endl;
    //status = db->Get(leveldb::ReadOptions(),key, &value);
    //if(!status.ok()) std::cerr<<key<<" "<<status.ToString()<<std::endl;
    //else std::cout<<key<<"==="<<value<<std::endl;
    //delete db;
    //return 0;
